package cn.yeyiu.sexyiu.activity;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import cn.yeyiu.mangaviewer.MangaDetailActivity;
import cn.yeyiu.sexyiu.R;
import cn.yeyiu.sexyiu.adapter.MangaAdapter;
import cn.yeyiu.sexyiu.listener.OnItemClickListener;
import cn.yeyiu.util.MediaUtil;
import cn.yeyiu.util.VideoUtil;
import cn.yeyiu.util.bean.MangaDetail;

public class MangaActivity extends AppCompatActivity {
    private MangaAdapter adapter;
    private ImageView backBtn;
    private TextView backTv;
    private ImageView btnNext;
    private ImageView btnPre;
    private View llPage;
    private RecyclerView recyclerView;
    private TextView tvCur;
    private TextView tvMax;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_manga);
        bindView();
        bindAdapter();
        bindEvent();
        initData();
    }

    void initData() {
        this.backTv.setText(getIntent().getStringExtra("ctg"));
        VideoUtil.postMangaList(this.adapter.mangaInfoList);
    }

    void bindView() {
        this.recyclerView = (RecyclerView) findViewById(R.id.manga_rv);
        this.backTv = (TextView) findViewById(R.id.back_tv);
        this.backBtn = (ImageView) findViewById(R.id.back_btn);
        this.tvMax = (TextView) findViewById(R.id.sum_page_tv);
        this.tvCur = (TextView) findViewById(R.id.cur_page_tv);
        this.btnPre = (ImageView) findViewById(R.id.pre_page_btn);
        this.btnNext = (ImageView) findViewById(R.id.next_page_btn);
        View findViewById = findViewById(R.id.page_ll);
        this.llPage = findViewById;
        findViewById.setVisibility(View.VISIBLE);
    }

    void bindEvent() {
        this.backBtn.setOnClickListener(v -> finish());
        this.adapter.mangaInfoList.observe(this, mangaInfos -> {
            this.adapter.update();
            this.tvMax.setText(this.adapter.getMaxPage());
        });
        this.btnPre.setOnClickListener(v -> {
            this.recyclerView.smoothScrollToPosition(0);
            this.adapter.prePage();
            this.tvCur.setText(this.adapter.getPage());
        });
        this.btnNext.setOnClickListener(v -> {
            this.recyclerView.smoothScrollToPosition(0);
            this.adapter.nextPage();
            this.tvCur.setText(this.adapter.getPage());
        });
        this.adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemLongClick(View view) {
            }
            @Override
            public void onItemClick(View view) {
                MangaDetail mangaDetail = MangaActivity.this.adapter.mangaInfoList.getValue().get(
                        (MangaActivity.this.adapter.cur_page * MangaActivity.this.adapter.page_size) + MangaActivity.this.recyclerView.getChildAdapterPosition(view));
                MediaUtil.readManga(MangaActivity.this, MangaDetailActivity.class, mangaDetail);
            }
        });
    }

    void bindAdapter() {
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        if (displayMetrics.widthPixels > displayMetrics.heightPixels) {
            this.recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        } else {
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
            this.recyclerView.setLayoutManager(linearLayoutManager);
        }
        MangaAdapter mangaAdapter = new MangaAdapter(this);
        this.adapter = mangaAdapter;
        this.recyclerView.setAdapter(mangaAdapter);
    }

}
